package cn.sgjk.train.member.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.log.Log;
import cn.sgjk.train.common.exception.BusinessException;
import cn.sgjk.train.common.exception.BusinessExceptionEnum;
import cn.sgjk.train.common.util.JwtUtil;
import cn.sgjk.train.common.util.SnowUtil;
import cn.sgjk.train.member.domain.Member;
import cn.sgjk.train.member.domain.MemberExample;
import cn.sgjk.train.member.mapper.MemberMapper;
import cn.sgjk.train.member.req.MemberLoginReq;
import cn.sgjk.train.member.req.MemberSendCodeReq;
import cn.sgjk.train.member.resp.MemberLoginResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;
import java.util.Map;

@Service
public class MemberService {


    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);
    @Autowired
    private MemberMapper memberMapper;

    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    public long register(String mobile) {
        Member memberDb = selectByMobile(mobile);

        if(ObjectUtil.isNotNull(memberDb)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);

        return member.getId();
    }

    public void sendCode(MemberSendCodeReq memberReq) {

        Member memberDb = selectByMobile(memberReq.getMobile());

        //如果手机不存在，则插入一条记录
       if(ObjectUtil.isNull(memberDb)){
           Member member = new Member();
           member.setId(SnowUtil.getSnowflakeNextId());
           member.setMobile(memberReq.getMobile());
           memberMapper.insert(member);
           LOG.info("插入一条手机信息");

       }

       //生成验证码
        String code = "8888";
        LOG.info("生成验证码:{}",code);

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        LOG.info("保存短信记录表");

        // 对接短信通道，发送短信
        LOG.info("对接短信通道");


    }

    public MemberLoginResp login(MemberLoginReq loginReq) {
        Member memberDb = selectByMobile(loginReq.getMobile());
        String code = loginReq.getCode();

        //如果手机不存在，则插入一条记录
        if(ObjectUtil.isNull(memberDb)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        //校验验证码
        if(!"8888".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_CODE_ERROR);
        }


        //hutool工具包生成jwt token
        MemberLoginResp resp = BeanUtil.copyProperties(memberDb,MemberLoginResp.class);
        Map<String, Object> map = BeanUtil.beanToMap(resp);
        String token = JwtUtil.createToken(memberDb.getId(),memberDb.getMobile());
        resp.setToken(token);
        return resp;
    }

    private Member selectByMobile(String memberReq) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(memberReq);
        List<Member> list = memberMapper.selectByExample(example);

        if(CollUtil.isEmpty(list)){
            LOG.info("手机号码不存在,自动注册");
            return null;
        }else {
            LOG.info("手机号码已存在");
            return list.get(0);
        }

    }


}

